package com.yq.front.action;

import com.yq.api.comm.ResultHandle;
import com.yq.front.service.LoginFrontService;
import com.yq.front.util.ViewPathConstants;
import com.yq.front.vo.UserVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;

/**
 * Created by yq on 16-7-30.
 */
@Controller("loginAction")
@SessionAttributes("currentUser")
public class LoginAction {
    private static final Log log = LogFactory.getLog(LoginAction.class);

    @Resource(name = "loginFrontService")
    private LoginFrontService loginFrontService;

    /**
     * 转到登陆页面
     * */
    @RequestMapping("gotoLogin.do")
    public String gotoLogin(){
        return ViewPathConstants.LOGIN_PAGE;
    }

    /**
     * 登陆
     * */
    @RequestMapping("login.do")
    public String login(UserVO currentUser, Model model){
        ResultHandle resultHandle = loginFrontService.doLogin(currentUser);
        if(resultHandle.getSuccess() == null || !resultHandle.getSuccess()){
            model.addAttribute("msg", resultHandle.getMessage());
            log.error("Authenticate user failed, message is " + resultHandle.getMessage());
            return ViewPathConstants.ERROR_PAGE;
        }
        model.addAttribute("currentUser", currentUser);
        return ViewPathConstants.UPDATE_USER_PAGE;
    }
}
